import datetime
from mrjob.job import MRJob
from mrjob.protocol import TextProtocol


class HourBehavior(MRJob):

    # OUTPUT_PROTOCOL = TextProtocol

    def mapper(self, _, line):
        lineinfo = line.strip().split(',')
        if len(lineinfo) == 6:
            if lineinfo[4] == '2017-12-02':
                hour = datetime.datetime.strptime(lineinfo[5], "%H:%M:%S").hour
                yield hour, (lineinfo[3], 1)

    def reducer(self, key, values):

        dict = {'pv':  0, 'buy': 0, 'cart': 0, 'fav': 0}

        for (behavior, counts) in values:
            dict[behavior] = dict[behavior] + counts
        # value_str = ''
        #
        # for num in dict.values():
        #     value_str += str(num) + '\t'

        yield key, [i for i in dict.values()]


if __name__ == '__main__':
    HourBehavior.run()
